home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 1 / Gold Medal Software Volume 1 (Gold Medal) (1994).iso / prog / tpwprog3.arj / UPLAY.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-07-02  |  3.2 KB  |  113 lines

  1. {******************************************************************}
  2. {                                                                  }
  3. {     Mancala                                                      }
  4. {     Turbo Pascal for Windows                                     }
  5. {     Copyright (c) 1991 by Swan Software. All rights reserved.    }
  6. {                                                                  }
  7. {******************************************************************}
  8.  
  9. { uplay.pas -- Play action for Mancala }
  10.  
  11. unit UPlay;
  12.  
  13. interface
  14.  
  15. uses UGlobals, Idents;
  16.  
  17. procedure NewGame(var Side: Integer);
  18. function GetMove(Side: Integer; var Move: OneMove): Boolean;
  19. procedure PrepareReplay(Move: OneMove);
  20.  
  21.  
  22. implementation
  23.  
  24. uses WinTypes, WinProcs, WObjects, UMove, USearch;
  25.  
  26.  
  27. {- Save information required for instant replay }
  28.  
  29. procedure PrepareReplay(Move: OneMove);
  30. begin
  31.   ReplayBoard := MainPosition;
  32.   ReplayMove := Move;
  33.   ReplaySide := Side;
  34.   ReplayOk := true;
  35. end;
  36.  
  37.  
  38. {- Reinitialize global variables and display for a new game. }
  39.  
  40. procedure Newgame(var Side: Integer);
  41. var
  42.   I: Integer;
  43. begin
  44.   Side := Human;
  45.   CurrentMessage := Its_your_move;
  46.   HumanMove := -1;
  47.   SelfPlay := false;
  48.   ReplayOk := false;
  49.   with MainPosition do
  50.   begin
  51.     Win := false;
  52.     GoAgain := false;
  53.     for I := 0 to MaxCupIndex do
  54.       Gameboard[I] := PebblesPerCup;
  55.     Gameboard[CompKalah] := 0;
  56.     Gameboard[HumanKalah] := 0;
  57.   end;
  58.   PebblesDiv2 := ((MaxCups - 2) * PebblesPerCup) div 2;
  59.   InvalidateRect(Application^.MainWindow^.HWindow, nil, true);
  60. end;
  61.  
  62.  
  63. {- Return true if Move returned for selected Side. If Side = computer,
  64. then GetMove carries out a search for the best move. If Side = human,
  65. then GetMove checks that the currently selected cup is legal. }
  66.  
  67. function GetMove(Side: Integer; var Move: OneMove): Boolean;
  68. var
  69.   MoveList: ListRec;
  70.   Score: Integer;
  71.   Msg: TMsg;
  72. begin
  73.   GetMove := false;
  74.   MoveIndex := 0;
  75.   if Side = computer then
  76.   begin
  77.     MoveGen(MainPosition, 1, MoveList);
  78.     if MoveList.Count <> 0 { Computer has at least one move } then
  79.     begin
  80.       BestMove := MoveStack[MoveList.FirstIndex].Move;
  81.       MaxMoveIndex := 0; { Used for debugging move search }
  82.       if (MaxPly > 0) and (MoveList.Count > 1) then
  83.       begin
  84.         LowLevel := 1;
  85.         Search(MainPosition, LowLevel, Score, MoveList, -maxint, +maxint);
  86.       end;
  87.       Move := BestMove;
  88.       GetMove := true;
  89.       {- Delete pending mouse and keyboard messages received during search }
  90.       while PeekMessage(Msg, 0, wm_MouseFirst, wm_MouseLast, pm_Remove) do ;
  91.       while PeekMessage(Msg, 0, wm_KeyFirst, wm_KeyLast, pm_Remove) do ;
  92.     end;
  93.   end else
  94.   if HumanMove <> -1 then
  95.   begin
  96.     Move := HumanMove;
  97.     HumanMove := -1;
  98.     MoveGen(MainPosition, 0, MoveList);
  99.     if MoveList.Count = 0 then Exit; { Human has no legal moves }
  100.     if LegalMove(Move, MoveList) then
  101.       GetMove := true
  102.   end;
  103. end;
  104.  
  105.  
  106. end.
  107.  
  108.  
  109. { ----------------------------------------------------------------
  110.   Copyright (c) 1991 by Swan Software. All rights reserved.
  111.   Revision 1.00    Date: 8/21/1991
  112.   ---------------------------------------------------------------- }
  113.